﻿using System;
using System.Security.Principal;

namespace Bricks.Security
{

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TGuard"></typeparam>
    public abstract class AuthorizationFacadeBase<TGuard> : FacadeBase<TGuard>, IAuthorizationFacade<TGuard>
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="guardAccessor"></param>
        protected AuthorizationFacadeBase(Func<TGuard> guardAccessor) :
            base(guardAccessor)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="principal"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public abstract bool Authorize(IPrincipal principal, string key);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="principal"></param>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public bool TryAuthorize(IPrincipal principal, string key, bool defaultValue)
        {
            try
            {
                return Authorize(principal, key);
            }
            catch
            {
                return defaultValue;
            }
        }
    }
}
